﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Project_Vital.Dao
{
    public class CustomerDao
    {

        const string connectionString = "Server=NEXTGEN-TECH01;Database=Vital;Trusted_Connection=True";

        public BindingList<Customer> GetCustomers()
        {
            ProjectVitalDataContext dateContext = new ProjectVitalDataContext(connectionString);
            List<vcus_vital_customer> linqCustomerList = dateContext.vcus_vital_customers.Select(customer => customer).ToList();

            BindingList<Customer> customerList = new BindingList<Customer>();
            linqCustomerList.ForEach(customer => customerList.Add(TransformLinqToBusinessCustomer(customer)));

            return customerList;
        }

        public void CreateCustomer(Customer customer)
        {
            vcus_vital_customer newCustomer = this.TranformCustomer(customer);
            ProjectVitalDataContext dateContext = new ProjectVitalDataContext(connectionString);
            dateContext.vcus_vital_customers.InsertOnSubmit(newCustomer);
            dateContext.SubmitChanges();
        }

        private Customer TransformLinqToBusinessCustomer(vcus_vital_customer customer)
        {
            Customer dbCustomer = new Customer();
            dbCustomer._vcus_customer_id = customer.vcus_customer_id;

            //dbCustomer._vcus_title = customer vcus_title;

            dbCustomer._vcus_first_name = customer.vcus_first_name;

            dbCustomer._vcus_surname = customer.vcus_surname;

            dbCustomer._vcus_dob = customer.vcus_dob;

            dbCustomer._vcus_contact_name = customer.vcus_contact_name;

            dbCustomer._vcus_business_name = customer.vcus_business_name;

            dbCustomer._vcus_business_addr1 = customer.vcus_business_addr1;

            dbCustomer._vcus_business_addr2 = customer.vcus_business_addr2;

            dbCustomer._vcus_city = customer.vcus_city;

            dbCustomer._vcus_postcode = customer.vcus_postcode;

            dbCustomer._vcus_phone = customer.vcus_phone;

            dbCustomer._vcus_mobile = customer.vcus_mobile;

            dbCustomer._vcus_fax = customer.vcus_fax;

            dbCustomer._vcus_email = customer.vcus_email;

            dbCustomer._vcus_open_time = customer.vcus_open_time;

            dbCustomer._vcus_close_time = customer.vcus_close_time;

            dbCustomer._vcus_account_start = customer.vcus_account_start;

            dbCustomer._vcus_account_end = customer.vcus_account_end;

            dbCustomer._vcus_invoice = customer.vcus_invoice;

            dbCustomer._vcus_pricing_level = customer.vcus_pricing_level;

            dbCustomer._vcus_credit = customer.vcus_credit;

            dbCustomer._vcus_credit_limit = customer.vcus_credit_limit;

            dbCustomer._vcus_credit_period = customer.vcus_credit_period;

            return dbCustomer;
        }

        private vcus_vital_customer TranformCustomer(Customer customer)
        {
            vcus_vital_customer dbCustomer = new vcus_vital_customer();
            
            dbCustomer.vcus_first_name = customer._vcus_first_name;

           dbCustomer.vcus_surname = customer._vcus_surname;

           dbCustomer.vcus_dob = customer._vcus_dob;

           dbCustomer.vcus_contact_name = customer._vcus_contact_name;

           dbCustomer.vcus_business_name = customer._vcus_business_name;

           dbCustomer.vcus_business_addr1 = customer._vcus_business_addr1;

           dbCustomer.vcus_business_addr2 = customer._vcus_business_addr2;

           dbCustomer.vcus_city = customer._vcus_city;

           dbCustomer.vcus_postcode = customer._vcus_postcode;

           dbCustomer.vcus_phone = customer._vcus_phone;

           dbCustomer.vcus_mobile = customer._vcus_mobile;

           dbCustomer.vcus_fax = customer._vcus_fax;

           dbCustomer.vcus_email = customer._vcus_email;

           dbCustomer.vcus_open_time = customer._vcus_open_time;

           dbCustomer.vcus_close_time = customer._vcus_close_time;

           dbCustomer.vcus_account_start = customer._vcus_account_start;

           dbCustomer.vcus_account_end = customer._vcus_account_end;

           dbCustomer.vcus_invoice = customer._vcus_invoice;

           dbCustomer.vcus_pricing_level = customer._vcus_pricing_level;

           dbCustomer.vcus_credit = customer._vcus_credit;

           dbCustomer.vcus_credit_limit= customer._vcus_credit_limit;

           dbCustomer.vcus_credit_period= customer._vcus_credit_period;

           return dbCustomer;
        }
    }
}
